package org.jeecg.modules.demo.weixin.smtmerchantprice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.api.UpmsHelloApi;
import org.jeecg.modules.demo.entity.Dept;
import org.jeecg.modules.demo.weixin.entity.SmtMerchantPrice;
import org.jeecg.modules.demo.weixin.smtmerchantprice.service.ISmtMerchantPriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Arrays;

/**
 * @Description: 金额记录表
 * @Author: jeecg-boot
 * @Date: 2023-12-27
 * @Version: V1.0
 */
@Api(tags = "金额记录表")
@RestController
@RequestMapping("/weixin/smtmerchantprice/smtMerchantPrice")
@Slf4j
public class SmtMerchantPriceController extends JeecgController<SmtMerchantPrice, ISmtMerchantPriceService> {
    @Autowired
    private ISmtMerchantPriceService smtMerchantPriceService;

    @Autowired
    private UpmsHelloApi upmsHelloApi;


    /**
     * 分页列表查询
     *
     * @param smtMerchantPrice
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "金额记录表-分页列表查询")
    @ApiOperation(value = "金额记录表-分页列表查询", notes = "金额记录表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<SmtMerchantPrice>> queryPageList(SmtMerchantPrice smtMerchantPrice,
                                                         @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                         HttpServletRequest req) {
        QueryWrapper<SmtMerchantPrice> queryWrapper = QueryGenerator.initQueryWrapper(smtMerchantPrice, req.getParameterMap());
        Page<SmtMerchantPrice> page = new Page<SmtMerchantPrice>(pageNo, pageSize);
        IPage<SmtMerchantPrice> pageList = smtMerchantPriceService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param smtMerchantPrice
     * @return
     */
    @AutoLog(value = "金额记录表-添加")
    @ApiOperation(value = "金额记录表-添加", notes = "金额记录表-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody SmtMerchantPrice smtMerchantPrice) {
        LambdaQueryWrapper<SmtMerchantPrice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SmtMerchantPrice::getMerchantId,smtMerchantPrice.getMerchantId());
        queryWrapper.eq(SmtMerchantPrice::getStastus,0);
        SmtMerchantPrice serviceOne = smtMerchantPriceService.getOne(queryWrapper);
        if (serviceOne!=null){
            return Result.error("你有一笔订单正在进行！");
        }
        if (smtMerchantPrice.getPrice().compareTo(BigDecimal.ZERO)<=0){
            return Result.error("金额等于0 不能进行提现");
        }
        smtMerchantPriceService.save(smtMerchantPrice);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param smtMerchantPrice
     * @return
     */
    @AutoLog(value = "金额记录表-编辑")
    @ApiOperation(value = "金额记录表-编辑", notes = "金额记录表-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody SmtMerchantPrice smtMerchantPrice) {
        smtMerchantPriceService.updateById(smtMerchantPrice);
        return Result.OK("已提交等待审核!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "金额记录表-通过id删除")
    @ApiOperation(value = "金额记录表-通过id删除", notes = "金额记录表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        smtMerchantPriceService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "金额记录表-批量删除")
    @ApiOperation(value = "金额记录表-批量删除", notes = "金额记录表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.smtMerchantPriceService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "金额记录表-通过id查询")
    @ApiOperation(value = "金额记录表-通过id查询", notes = "金额记录表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<SmtMerchantPrice> queryById(@RequestParam(name = "id", required = true) String id) {
        SmtMerchantPrice smtMerchantPrice = smtMerchantPriceService.getById(id);
        if (smtMerchantPrice == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(smtMerchantPrice);
    }

    @ApiOperation(value = "门店对应流动", notes = "门店对应流动")
    @GetMapping(value = "/queryMer")
    public Result<SmtMerchantPrice> queryMer(@RequestParam String merchantId) {
        LambdaQueryWrapper<SmtMerchantPrice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SmtMerchantPrice::getMerchantId, merchantId);
        SmtMerchantPrice smtMerchantPrice = smtMerchantPriceService.getOne(queryWrapper);
        if (smtMerchantPrice == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(smtMerchantPrice);
    }


    /**
     * 导出excel
     *
     * @param request
     * @param smtMerchantPrice
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SmtMerchantPrice smtMerchantPrice) {
        return super.exportXls(request, smtMerchantPrice, SmtMerchantPrice.class, "金额记录表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SmtMerchantPrice.class);
    }


//    /**
//     * 添加
//     *
//     * @param smtMerchantPrice
//     * @return
//     */
//    @AutoLog(value = "提现记录")
//    @ApiOperation(value = "提现记录", notes = "提现记录")
//    @PostMapping(value = "/addPriceRecord")
//    public Result<String> addPriceRecord(@RequestBody SmtMerchantPrice smtMerchantPrice) {
//        smtMerchantPrice.setStastus(0);
//        smtMerchantPrice.setType(1);
//        smtMerchantPriceService.save(smtMerchantPrice);
//    }


    @AutoLog(value = "提现状态修改")
    @ApiOperation(value = "提现状态修改", notes = "提现状态修改")
    @PostMapping(value = "/updatePriceRecord")
    public Result<String> updatePriceRecord(@RequestBody SmtMerchantPrice smtMerchantPrice) {
        SmtMerchantPrice merchantPrice = smtMerchantPriceService.getById(smtMerchantPrice.getId());
        if (merchantPrice != null && merchantPrice.getStastus().equals(0) ) {
            if (smtMerchantPrice.getStastus().equals(1)) {
                merchantPrice.setStastus(1);
                smtMerchantPriceService.updateById(merchantPrice);
                upmsHelloApi.updateDeptPric(Long.valueOf(smtMerchantPrice.getMerchantId()), smtMerchantPrice.getPrice());
            }
            if (smtMerchantPrice.getStastus().equals(2)) {
                merchantPrice.setStastus(2);
                merchantPrice.setReason(smtMerchantPrice.getReason());
                smtMerchantPriceService.updateById(merchantPrice);
            }
        }
        return Result.ok("成功");
    }


}
